<template><div><h2 id="设计实体字段的时候可以用基本数据类型吗" tabindex="-1"><a class="header-anchor" href="#设计实体字段的时候可以用基本数据类型吗"><span>设计实体字段的时候可以用基本数据类型吗</span></a></h2>
<p>比如你设计了一个订单类</p>
<div class="language-java line-numbers-mode" data-highlighter="shiki" data-ext="java" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#C678DD">public</span><span style="color:#C678DD"> class</span><span style="color:#E5C07B"> OrderLine</span><span style="color:#ABB2BF"> {</span></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">TableId</span><span style="color:#E06C75">(</span><span style="color:#D19A66">type</span><span style="color:#56B6C2"> =</span><span style="color:#E5C07B"> IdType</span><span style="color:#ABB2BF">.</span><span style="color:#E5C07B">AUTO</span><span style="color:#E06C75">)</span></span>
<span class="line"><span style="color:#C678DD">    private</span><span style="color:#E5C07B"> Long</span><span style="color:#E06C75"> id</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#E06C75">    </span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">    // 用了基本类型int</span></span>
<span class="line"><span style="color:#C678DD">    private</span><span style="color:#C678DD"> int</span><span style="color:#E06C75"> status</span><span style="color:#ABB2BF">;</span><span style="color:#7F848E;font-style:italic"> // 订单状态：1-待支付，2-已支付，3-已取消</span></span>
<span class="line"><span style="color:#E06C75">    </span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">    // 其他字段...</span></span>
<span class="line"><span style="color:#ABB2BF">}</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当我们去new对象的时候默认会把status赋值为0,这就造成了脏数据;如果我们此时调用更新方法,会把status进行了赋值更新.</p>
<blockquote>
<p><a href="https://mp.weixin.qq.com/s/zizos2_GAHuR3T61o06KgA" target="_blank" rel="noopener noreferrer">设计Entity字段可以用基本数据类型吗？</a></p>
</blockquote>
<h2 id="接口习惯" tabindex="-1"><a class="header-anchor" href="#接口习惯"><span>接口习惯</span></a></h2>
<ol>
<li>
<p>使用if进行参数校验</p>
</li>
<li>
<p>可以预想到的Exception异常 手动trycatch捕获,最后全局捕获其他未知的Exception,统一报未知错误</p>
</li>
</ol>
<p>GET接口DEMO</p>
<div class="language-java line-numbers-mode has-collapsed-lines collapsed" data-highlighter="shiki" data-ext="java" style="--vp-collapsed-lines:15;background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">ApiOperation</span><span style="color:#E06C75">(</span><span style="color:#98C379">"查找单个学生的成绩"</span><span style="color:#E06C75">)</span></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">GetMapping</span><span style="color:#E06C75">(</span><span style="color:#98C379">"/queryScoreById"</span><span style="color:#E06C75">)</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#E5C07B"> Result</span><span style="color:#61AFEF"> queryScoreById</span><span style="color:#E06C75">(</span></span>
<span class="line"><span style="color:#ABB2BF">            @</span><span style="color:#E5C07B">ApiParam</span><span style="color:#E06C75">(</span><span style="color:#D19A66">name</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "id"</span><span style="color:#ABB2BF">,</span><span style="color:#D19A66"> value</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "学生id"</span><span style="color:#ABB2BF">,</span><span style="color:#D19A66"> required</span><span style="color:#56B6C2"> =</span><span style="color:#D19A66"> true</span><span style="color:#E06C75">) </span><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">RequestParam</span><span style="color:#E5C07B"> Integer</span><span style="color:#E06C75"> id</span><span style="color:#ABB2BF">,</span></span>
<span class="line"><span style="color:#ABB2BF">            @</span><span style="color:#E5C07B">ApiParam</span><span style="color:#E06C75">(</span><span style="color:#D19A66">name</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "pageNumber"</span><span style="color:#ABB2BF">,</span><span style="color:#D19A66"> value</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "页数"</span><span style="color:#ABB2BF">,</span><span style="color:#D19A66"> defaultValue</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "1"</span><span style="color:#E06C75">) </span><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">RequestParam</span><span style="color:#E5C07B"> Integer</span><span style="color:#E06C75"> pageNumber</span><span style="color:#ABB2BF">,</span></span>
<span class="line"><span style="color:#ABB2BF">            @</span><span style="color:#E5C07B">ApiParam</span><span style="color:#E06C75">(</span><span style="color:#D19A66">name</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "pageSize"</span><span style="color:#ABB2BF">,</span><span style="color:#D19A66"> value</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "页面大小"</span><span style="color:#ABB2BF">,</span><span style="color:#D19A66"> defaultValue</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "10"</span><span style="color:#E06C75">) </span><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">RequestParam</span><span style="color:#E5C07B"> Integer</span><span style="color:#E06C75"> pageSize</span></span>
<span class="line"><span style="color:#E06C75">    ) {</span></span>
<span class="line"><span style="color:#C678DD">        if</span><span style="color:#E06C75"> (pageNumber </span><span style="color:#56B6C2">==</span><span style="color:#D19A66"> null</span><span style="color:#E06C75">) pageNumber </span><span style="color:#56B6C2">=</span><span style="color:#D19A66"> 1</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">        if</span><span style="color:#E06C75"> (pageSize </span><span style="color:#56B6C2">==</span><span style="color:#D19A66"> null</span><span style="color:#E06C75">) pageSize </span><span style="color:#56B6C2">=</span><span style="color:#D19A66"> 10</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">        if</span><span style="color:#E06C75"> (id </span><span style="color:#56B6C2">==</span><span style="color:#D19A66"> null</span><span style="color:#E06C75">) </span><span style="color:#C678DD">return</span><span style="color:#E5C07B"> ResultUtil</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">error</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">ResultCodeEnum</span><span style="color:#ABB2BF">.</span><span style="color:#E5C07B">ERROR</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">getCode</span><span style="color:#ABB2BF">(), </span><span style="color:#98C379">"缺少必填参数"</span><span style="color:#ABB2BF">);</span></span>
<span class="line"><span style="color:#E5C07B">        QueryWrapper</span><span style="color:#ABB2BF">&#x3C;</span><span style="color:#E5C07B">TeachCaseStudent</span><span style="color:#ABB2BF">></span><span style="color:#E06C75"> queryWrapper </span><span style="color:#56B6C2">=</span><span style="color:#C678DD"> new</span><span style="color:#E5C07B"> QueryWrapper</span><span style="color:#ABB2BF">&#x3C;></span><span style="color:#E06C75">()</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#E5C07B">        queryWrapper</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">eq</span><span style="color:#ABB2BF">(</span><span style="color:#98C379">"student_id"</span><span style="color:#ABB2BF">, id);</span></span>
<span class="line"><span style="color:#E5C07B">        Page</span><span style="color:#ABB2BF">&#x3C;</span><span style="color:#E5C07B">TeachCaseStudent</span><span style="color:#ABB2BF">></span><span style="color:#E06C75"> resultPage </span><span style="color:#56B6C2">=</span><span style="color:#C678DD"> new</span><span style="color:#E5C07B"> Page</span><span style="color:#ABB2BF">&#x3C;></span><span style="color:#E06C75">(pageNumber</span><span style="color:#ABB2BF">,</span><span style="color:#E06C75"> pageSize)</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#E5C07B">        teachCaseStudentService</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">page</span><span style="color:#ABB2BF">(resultPage, queryWrapper);</span></span>
<span class="line"><span style="color:#C678DD">        return</span><span style="color:#E5C07B"> ResultUtil</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">success</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">ResultCodeEnum</span><span style="color:#ABB2BF">.</span><span style="color:#E5C07B">SUCCESS</span><span style="color:#ABB2BF">, resultPage);</span></span>
<span class="line"><span style="color:#E06C75">    }</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div><div class="collapsed-lines"></div></div><p>POST接口DEMO</p>
<div class="language-java line-numbers-mode" data-highlighter="shiki" data-ext="java" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">ApiOperation</span><span style="color:#E06C75">(</span><span style="color:#98C379">"查看学生答题成绩"</span><span style="color:#E06C75">)</span></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">PostMapping</span><span style="color:#E06C75">(</span><span style="color:#98C379">"/queryTeachCaseStudentList"</span><span style="color:#E06C75">)</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#E5C07B"> Result</span><span style="color:#61AFEF"> queryTeachCaseStudentList</span><span style="color:#E06C75">(</span><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">RequestBody</span><span style="color:#E5C07B"> QueryTeachCaseStudentListVo</span><span style="color:#E06C75"> queryTeachCaseStudentListVo) {</span></span>
<span class="line"><span style="color:#E5C07B">        QueryWrapper</span><span style="color:#ABB2BF">&#x3C;</span><span style="color:#E5C07B">Account</span><span style="color:#ABB2BF">></span><span style="color:#E06C75"> classQueryWrapper </span><span style="color:#56B6C2">=</span><span style="color:#C678DD"> new</span><span style="color:#E5C07B"> QueryWrapper</span><span style="color:#ABB2BF">&#x3C;></span><span style="color:#E06C75">()</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#E5C07B">        QueryWrapper</span><span style="color:#ABB2BF">&#x3C;</span><span style="color:#E5C07B">TeachCaseStudent</span><span style="color:#ABB2BF">></span><span style="color:#E06C75"> idQueryWrapper </span><span style="color:#56B6C2">=</span><span style="color:#C678DD"> new</span><span style="color:#E5C07B"> QueryWrapper</span><span style="color:#ABB2BF">&#x3C;></span><span style="color:#E06C75">()</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">        if</span><span style="color:#E06C75"> (</span><span style="color:#E5C07B">queryTeachCaseStudentListVo</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">getClassCode</span><span style="color:#ABB2BF">()</span><span style="color:#56B6C2"> !=</span><span style="color:#D19A66"> null</span><span style="color:#E06C75">)</span></span>
<span class="line"><span style="color:#E5C07B">            classQueryWrapper</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">eq</span><span style="color:#ABB2BF">(</span><span style="color:#98C379">"class_code"</span><span style="color:#ABB2BF">, </span><span style="color:#E5C07B">queryTeachCaseStudentListVo</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">getClassCode</span><span style="color:#ABB2BF">());</span></span>
<span class="line"><span style="color:#E5C07B">        List</span><span style="color:#ABB2BF">&#x3C;</span><span style="color:#E5C07B">Long</span><span style="color:#ABB2BF">></span><span style="color:#E06C75"> idList </span><span style="color:#56B6C2">=</span><span style="color:#E5C07B"> accountService</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">list</span><span style="color:#ABB2BF">(classQueryWrapper).</span><span style="color:#61AFEF">stream</span><span style="color:#ABB2BF">().</span><span style="color:#61AFEF">map</span><span style="color:#ABB2BF">(Account</span><span style="color:#C678DD">::</span><span style="color:#ABB2BF">getId).</span><span style="color:#61AFEF">collect</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">Collectors</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">toList</span><span style="color:#ABB2BF">());</span></span>
<span class="line"><span style="color:#C678DD">        if</span><span style="color:#E06C75"> (</span><span style="color:#56B6C2">!</span><span style="color:#E5C07B">idList</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">isEmpty</span><span style="color:#ABB2BF">()</span><span style="color:#E06C75">) </span><span style="color:#E5C07B">idQueryWrapper</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">in</span><span style="color:#ABB2BF">(</span><span style="color:#98C379">"student_id"</span><span style="color:#ABB2BF">, idList);</span></span>
<span class="line"><span style="color:#C678DD">        else</span><span style="color:#C678DD"> return</span><span style="color:#E5C07B"> ResultUtil</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">success</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">ResultCodeEnum</span><span style="color:#ABB2BF">.</span><span style="color:#E5C07B">SUCCESS</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">getCode</span><span style="color:#ABB2BF">(), </span><span style="color:#98C379">"查询结果为空"</span><span style="color:#ABB2BF">);</span></span>
<span class="line"><span style="color:#E5C07B">        Page</span><span style="color:#ABB2BF">&#x3C;</span><span style="color:#E5C07B">TeachCaseStudent</span><span style="color:#ABB2BF">></span><span style="color:#E06C75"> resultPage </span><span style="color:#56B6C2">=</span><span style="color:#C678DD"> new</span><span style="color:#E5C07B"> Page</span><span style="color:#ABB2BF">&#x3C;></span><span style="color:#E06C75">(</span><span style="color:#E5C07B">queryTeachCaseStudentListVo</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">getPageNumber</span><span style="color:#ABB2BF">(),</span><span style="color:#E5C07B"> queryTeachCaseStudentListVo</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">getPageSize</span><span style="color:#ABB2BF">()</span><span style="color:#E06C75">)</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#E5C07B">        teachCaseStudentService</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">page</span><span style="color:#ABB2BF">(resultPage, idQueryWrapper);</span></span>
<span class="line"><span style="color:#C678DD">        return</span><span style="color:#E5C07B"> ResultUtil</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">success</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">ResultCodeEnum</span><span style="color:#ABB2BF">.</span><span style="color:#E5C07B">SUCCESS</span><span style="color:#ABB2BF">, resultPage);</span></span>
<span class="line"><span style="color:#E06C75">    }</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>请求体参数DEMO</p>
<div class="language-java line-numbers-mode" data-highlighter="shiki" data-ext="java" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">Data</span></span>
<span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">ApiModel</span><span style="color:#E06C75">(</span><span style="color:#D19A66">description</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "查询学生成绩参数对象"</span><span style="color:#E06C75">)</span></span>
<span class="line"><span style="color:#C678DD">public</span><span style="color:#C678DD"> class</span><span style="color:#E5C07B"> QueryTeachCaseStudentListVo</span><span style="color:#ABB2BF"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">ApiModelProperty</span><span style="color:#E06C75">(</span><span style="color:#D19A66">value</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "班级编码"</span><span style="color:#ABB2BF">,</span><span style="color:#D19A66"> example</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "2800280004"</span><span style="color:#E06C75">)</span></span>
<span class="line"><span style="color:#C678DD">    private</span><span style="color:#E5C07B"> String</span><span style="color:#E06C75"> classCode</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">ApiModelProperty</span><span style="color:#E06C75">(</span><span style="color:#D19A66">value</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "页数"</span><span style="color:#ABB2BF">,</span><span style="color:#D19A66"> example</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "1"</span><span style="color:#E06C75">)</span></span>
<span class="line"><span style="color:#C678DD">    private</span><span style="color:#E5C07B"> Integer</span><span style="color:#E06C75"> pageNumber </span><span style="color:#56B6C2">=</span><span style="color:#D19A66"> 1</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">ApiModelProperty</span><span style="color:#E06C75">(</span><span style="color:#D19A66">value</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "分页大小"</span><span style="color:#ABB2BF">,</span><span style="color:#D19A66"> example</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "10"</span><span style="color:#E06C75">)</span></span>
<span class="line"><span style="color:#C678DD">    private</span><span style="color:#E5C07B"> Integer</span><span style="color:#E06C75"> pageSize </span><span style="color:#56B6C2">=</span><span style="color:#D19A66"> 10</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">}</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="常用注解的使用" tabindex="-1"><a class="header-anchor" href="#常用注解的使用"><span>常用注解的使用</span></a></h2>
<h3 id="api" tabindex="-1"><a class="header-anchor" href="#api"><span>@Api</span></a></h3>
<ul>
<li>tags 分类标签</li>
<li>description 接口描述</li>
</ul>
<p>DEMO</p>
<div class="language-java line-numbers-mode" data-highlighter="shiki" data-ext="java" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">Api</span><span style="color:#E06C75">(</span><span style="color:#D19A66">tags</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "01.听证模拟"</span><span style="color:#ABB2BF">,</span><span style="color:#D19A66"> description</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "听证模拟模块接口"</span><span style="color:#E06C75">)</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="apioperation" tabindex="-1"><a class="header-anchor" href="#apioperation"><span>@ApiOperation</span></a></h3>
<p>DEMO</p>
<div class="language-java line-numbers-mode" data-highlighter="shiki" data-ext="java" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">ApiOperation</span><span style="color:#E06C75">(</span><span style="color:#98C379">"对接口的描述"</span><span style="color:#E06C75">)</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="apiparam" tabindex="-1"><a class="header-anchor" href="#apiparam"><span>@ApiParam</span></a></h3>
<ul>
<li>name 参数名</li>
<li>value 参数描述</li>
<li>defaultValue 默认值</li>
<li>required 是否必填</li>
<li>example 举例参数值</li>
<li>paramType 参数类型</li>
<li>dataType 参数的数据类型</li>
</ul>
<p>DEMO</p>
<p>见接口习惯</p>
<h3 id="apimodelproperty" tabindex="-1"><a class="header-anchor" href="#apimodelproperty"><span>@ApiModelProperty</span></a></h3>
<ul>
<li>name 参数名(基本不写)</li>
<li>value 字段描述</li>
<li>example 示例值</li>
</ul>
<p>DEMO</p>
<div class="language-java line-numbers-mode" data-highlighter="shiki" data-ext="java" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">ApiModelProperty</span><span style="color:#E06C75">(</span><span style="color:#D19A66">value</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "页数"</span><span style="color:#ABB2BF">,</span><span style="color:#D19A66"> example</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "1"</span><span style="color:#E06C75">)</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="apimodel-基本不写" tabindex="-1"><a class="header-anchor" href="#apimodel-基本不写"><span>@ApiModel(基本不写)</span></a></h3>
<ul>
<li>value 对象名</li>
<li>description 描述</li>
</ul>
<p>DEMO</p>
<div class="language-java line-numbers-mode" data-highlighter="shiki" data-ext="java" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">ApiModel</span><span style="color:#E06C75">(</span><span style="color:#D19A66">description</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "描述对象作用"</span><span style="color:#E06C75">)</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="对象值拷贝" tabindex="-1"><a class="header-anchor" href="#对象值拷贝"><span>对象值拷贝</span></a></h2>
<div class="language-java line-numbers-mode" data-highlighter="shiki" data-ext="java" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#7F848E;font-style:italic">/**</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * 工具类拷贝对象属性</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * 同名同类型的会被赋值</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> */</span></span>
<span class="line"><span style="color:#E5C07B">BeanUtils</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">copyProperties</span><span style="color:#ABB2BF">(addProductDTO,product);</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="全局统一与异常处理" tabindex="-1"><a class="header-anchor" href="#全局统一与异常处理"><span>全局统一与异常处理</span></a></h2>
<h3 id="resultvo" tabindex="-1"><a class="header-anchor" href="#resultvo"><span>ResultVO</span></a></h3>
<p><strong>结果封装类</strong></p>
<div class="language-java line-numbers-mode has-collapsed-lines collapsed" data-highlighter="shiki" data-ext="java" style="--vp-collapsed-lines:15;background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#C678DD">package</span><span style="color:#C678DD"> xyz.hmz.springbootdemo1.vo</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> lombok.AllArgsConstructor</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> lombok.Data</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic">/**</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @Author haomingze</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @Date 2025/3/10 11:23</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @description: 全局返回结果封装</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> */</span></span>
<span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">Data</span></span>
<span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">AllArgsConstructor</span></span>
<span class="line"><span style="color:#C678DD">public</span><span style="color:#C678DD"> class</span><span style="color:#E5C07B"> ResultVO</span><span style="color:#ABB2BF"> {</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">    /**</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">     * 业务的状态值</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">     * 0 表示正常</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">     * 1 表示有问题</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">     */</span></span>
<span class="line"><span style="color:#C678DD">    private</span><span style="color:#E5C07B">  Integer</span><span style="color:#E06C75"> code</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">    /**</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">     * 对状态值的描述</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">     */</span></span>
<span class="line"><span style="color:#C678DD">    private</span><span style="color:#E5C07B">  String</span><span style="color:#E06C75"> message</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">    /**</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">     * 携带的数据</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic">     */</span></span>
<span class="line"><span style="color:#C678DD">    private</span><span style="color:#E5C07B">  Object</span><span style="color:#E06C75"> data</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#61AFEF"> ResultVO</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">Integer</span><span style="color:#E06C75;font-style:italic"> code</span><span style="color:#ABB2BF">,</span><span style="color:#E5C07B">String</span><span style="color:#E06C75;font-style:italic"> message</span><span style="color:#ABB2BF">){</span></span>
<span class="line"><span style="color:#E5C07B">        this</span><span style="color:#ABB2BF">.</span><span style="color:#E5C07B">code</span><span style="color:#56B6C2"> =</span><span style="color:#ABB2BF"> code;</span></span>
<span class="line"><span style="color:#E5C07B">        this</span><span style="color:#ABB2BF">.</span><span style="color:#E5C07B">message</span><span style="color:#56B6C2"> =</span><span style="color:#ABB2BF"> message;</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"><span style="color:#C678DD">    private</span><span style="color:#C678DD"> static</span><span style="color:#C678DD"> final</span><span style="color:#E5C07B"> ResultVO</span><span style="color:#E06C75"> DEFAULT_SUCCESS </span><span style="color:#56B6C2">=</span><span style="color:#C678DD"> new</span><span style="color:#61AFEF"> ResultVO</span><span style="color:#E06C75">(</span><span style="color:#D19A66">0</span><span style="color:#ABB2BF">,</span><span style="color:#98C379">"操作成功"</span><span style="color:#E06C75">)</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">    private</span><span style="color:#C678DD"> static</span><span style="color:#C678DD"> final</span><span style="color:#E5C07B"> ResultVO</span><span style="color:#E06C75"> DEFAULT_FAIL </span><span style="color:#56B6C2">=</span><span style="color:#C678DD"> new</span><span style="color:#61AFEF"> ResultVO</span><span style="color:#E06C75">(</span><span style="color:#D19A66">1</span><span style="color:#ABB2BF">,</span><span style="color:#98C379">"操作失败"</span><span style="color:#E06C75">)</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#C678DD"> static</span><span style="color:#E5C07B"> ResultVO</span><span style="color:#61AFEF"> success</span><span style="color:#ABB2BF">(){</span></span>
<span class="line"><span style="color:#C678DD">        return</span><span style="color:#ABB2BF"> DEFAULT_SUCCESS;</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#C678DD"> static</span><span style="color:#E5C07B"> ResultVO</span><span style="color:#61AFEF"> fail</span><span style="color:#ABB2BF">(){</span></span>
<span class="line"><span style="color:#C678DD">        return</span><span style="color:#ABB2BF"> DEFAULT_FAIL;</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#C678DD"> static</span><span style="color:#E5C07B"> ResultVO</span><span style="color:#61AFEF"> success</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">String</span><span style="color:#E06C75;font-style:italic"> message</span><span style="color:#ABB2BF">){</span></span>
<span class="line"><span style="color:#C678DD">        return</span><span style="color:#C678DD"> new</span><span style="color:#61AFEF"> ResultVO</span><span style="color:#ABB2BF">(</span><span style="color:#D19A66">0</span><span style="color:#ABB2BF">,message);</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#C678DD"> static</span><span style="color:#E5C07B"> ResultVO</span><span style="color:#61AFEF"> fail</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">String</span><span style="color:#E06C75;font-style:italic"> message</span><span style="color:#ABB2BF">){</span></span>
<span class="line"><span style="color:#C678DD">        return</span><span style="color:#C678DD"> new</span><span style="color:#61AFEF"> ResultVO</span><span style="color:#ABB2BF">(</span><span style="color:#D19A66">1</span><span style="color:#ABB2BF">,message);</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#C678DD"> static</span><span style="color:#E5C07B"> ResultVO</span><span style="color:#61AFEF"> success</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">Object</span><span style="color:#E06C75;font-style:italic"> data</span><span style="color:#ABB2BF">){</span></span>
<span class="line"><span style="color:#C678DD">        return</span><span style="color:#61AFEF"> success</span><span style="color:#ABB2BF">(</span><span style="color:#98C379">"操作成功"</span><span style="color:#ABB2BF">,data);</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#C678DD"> static</span><span style="color:#E5C07B"> ResultVO</span><span style="color:#61AFEF"> fail</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">Object</span><span style="color:#E06C75;font-style:italic"> data</span><span style="color:#ABB2BF">){</span></span>
<span class="line"><span style="color:#C678DD">        return</span><span style="color:#61AFEF"> fail</span><span style="color:#ABB2BF">(</span><span style="color:#98C379">"操作失败"</span><span style="color:#ABB2BF">,data);</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#C678DD"> static</span><span style="color:#E5C07B"> ResultVO</span><span style="color:#61AFEF"> success</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">String</span><span style="color:#E06C75;font-style:italic"> message</span><span style="color:#ABB2BF">,</span><span style="color:#E5C07B">Object</span><span style="color:#E06C75;font-style:italic"> data</span><span style="color:#ABB2BF">){</span></span>
<span class="line"><span style="color:#C678DD">        return</span><span style="color:#C678DD"> new</span><span style="color:#61AFEF"> ResultVO</span><span style="color:#ABB2BF">(</span><span style="color:#D19A66">0</span><span style="color:#ABB2BF">,message,data);</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#C678DD"> static</span><span style="color:#E5C07B"> ResultVO</span><span style="color:#61AFEF"> fail</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">String</span><span style="color:#E06C75;font-style:italic"> message</span><span style="color:#ABB2BF">,</span><span style="color:#E5C07B">Object</span><span style="color:#E06C75;font-style:italic"> data</span><span style="color:#ABB2BF">){</span></span>
<span class="line"><span style="color:#C678DD">        return</span><span style="color:#C678DD"> new</span><span style="color:#61AFEF"> ResultVO</span><span style="color:#ABB2BF">(</span><span style="color:#D19A66">1</span><span style="color:#ABB2BF">,message,data);</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"><span style="color:#ABB2BF">}</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div><div class="collapsed-lines"></div></div><h3 id="resultcontrolleradvice" tabindex="-1"><a class="header-anchor" href="#resultcontrolleradvice"><span>ResultControllerAdvice</span></a></h3>
<p><strong>返回体拦截，统一返回</strong>个人感觉没必要，容易造成返回混乱</p>
<div class="language-java line-numbers-mode has-collapsed-lines collapsed" data-highlighter="shiki" data-ext="java" style="--vp-collapsed-lines:15;background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#C678DD">package</span><span style="color:#C678DD"> xyz.hmz.springbootdemo1.controller</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> com.fasterxml.jackson.databind.ObjectMapper</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> lombok.SneakyThrows</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.beans.factory.annotation.Autowired</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.core.MethodParameter</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.http.MediaType</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.http.converter.HttpMessageConverter</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.http.server.ServerHttpRequest</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.http.server.ServerHttpResponse</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.web.bind.annotation.RestControllerAdvice</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> xyz.hmz.springbootdemo1.vo.ResultVO</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic">/**</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @Author haomingze</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @Date 2025/3/10 15:32</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @description: 全局结果统一拦截</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> */</span></span>
<span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">RestControllerAdvice</span></span>
<span class="line"><span style="color:#C678DD">public</span><span style="color:#C678DD"> class</span><span style="color:#E5C07B"> ResultControllerAdvice</span><span style="color:#C678DD"> implements</span><span style="color:#E5C07B"> ResponseBodyAdvice</span><span style="color:#ABB2BF">&#x3C;</span><span style="color:#E5C07B">Object</span><span style="color:#ABB2BF">></span><span style="color:#ABB2BF"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">Autowired</span></span>
<span class="line"><span style="color:#C678DD">    private</span><span style="color:#E5C07B"> ObjectMapper</span><span style="color:#E06C75"> objectMapper</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">Override</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#C678DD"> boolean</span><span style="color:#61AFEF"> supports</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">MethodParameter</span><span style="color:#E06C75;font-style:italic"> returnType</span><span style="color:#ABB2BF">, </span><span style="color:#E5C07B">Class</span><span style="color:#ABB2BF">&#x3C;</span><span style="color:#C678DD">?</span><span style="color:#C678DD"> extends</span><span style="color:#E5C07B"> HttpMessageConverter</span><span style="color:#ABB2BF">&#x3C;</span><span style="color:#C678DD">?</span><span style="color:#ABB2BF">>> </span><span style="color:#E06C75;font-style:italic">converterType</span><span style="color:#ABB2BF">)</span><span style="color:#ABB2BF"> {</span></span>
<span class="line"><span style="color:#C678DD">        return</span><span style="color:#D19A66"> true</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">SneakyThrows</span></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">Override</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#E5C07B"> Object</span><span style="color:#61AFEF"> beforeBodyWrite</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">Object</span><span style="color:#E06C75;font-style:italic"> body</span><span style="color:#ABB2BF">, </span><span style="color:#E5C07B">MethodParameter</span><span style="color:#E06C75;font-style:italic"> returnType</span><span style="color:#ABB2BF">, </span><span style="color:#E5C07B">MediaType</span><span style="color:#E06C75;font-style:italic"> selectedContentType</span><span style="color:#ABB2BF">, </span><span style="color:#E5C07B">Class</span><span style="color:#ABB2BF">&#x3C;</span><span style="color:#C678DD">?</span><span style="color:#C678DD"> extends</span><span style="color:#E5C07B"> HttpMessageConverter</span><span style="color:#ABB2BF">&#x3C;</span><span style="color:#C678DD">?</span><span style="color:#ABB2BF">>> </span><span style="color:#E06C75;font-style:italic">selectedConverterType</span><span style="color:#ABB2BF">, </span><span style="color:#E5C07B">ServerHttpRequest</span><span style="color:#E06C75;font-style:italic"> request</span><span style="color:#ABB2BF">, </span><span style="color:#E5C07B">ServerHttpResponse</span><span style="color:#E06C75;font-style:italic"> response</span><span style="color:#ABB2BF">)</span><span style="color:#ABB2BF"> {</span></span>
<span class="line"><span style="color:#C678DD">        if</span><span style="color:#ABB2BF">(body </span><span style="color:#C678DD">instanceof</span><span style="color:#ABB2BF"> String) </span><span style="color:#C678DD">return</span><span style="color:#E5C07B"> objectMapper</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">writeValueAsString</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">ResultVO</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">success</span><span style="color:#ABB2BF">(body));</span></span>
<span class="line"><span style="color:#C678DD">        if</span><span style="color:#ABB2BF">(body </span><span style="color:#C678DD">instanceof</span><span style="color:#ABB2BF"> ResultVO) </span><span style="color:#C678DD">return</span><span style="color:#ABB2BF"> body;</span></span>
<span class="line"><span style="color:#C678DD">        return</span><span style="color:#E5C07B"> ResultVO</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">success</span><span style="color:#ABB2BF">(body);</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"><span style="color:#ABB2BF">}</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div><div class="collapsed-lines"></div></div><h3 id="exceptioncontrolleradvice" tabindex="-1"><a class="header-anchor" href="#exceptioncontrolleradvice"><span>ExceptionControllerAdvice</span></a></h3>
<p><strong>接口层异常捕获</strong></p>
<div class="language-java line-numbers-mode has-collapsed-lines collapsed" data-highlighter="shiki" data-ext="java" style="--vp-collapsed-lines:15;background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#C678DD">package</span><span style="color:#C678DD"> xyz.hmz.springbootdemo1.controller</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.web.bind.annotation.ExceptionHandler</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.web.bind.annotation.RestControllerAdvice</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> xyz.hmz.springbootdemo1.vo.ResultVO</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic">/**</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @Author haomingze</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @Date 2025/3/3 11:25</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @description: 全局异常捕获</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> */</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">RestControllerAdvice</span></span>
<span class="line"><span style="color:#C678DD">public</span><span style="color:#C678DD"> class</span><span style="color:#E5C07B"> ExceptionControllerAdvice</span><span style="color:#ABB2BF"> {</span></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">ExceptionHandler</span><span style="color:#E06C75">(</span><span style="color:#E5C07B">Exception</span><span style="color:#ABB2BF">.</span><span style="color:#E5C07B">class</span><span style="color:#E06C75">)</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#E5C07B"> ResultVO</span><span style="color:#61AFEF"> handle</span><span style="color:#ABB2BF">(){</span></span>
<span class="line"><span style="color:#C678DD">        return</span><span style="color:#E5C07B"> ResultVO</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">fail</span><span style="color:#ABB2BF">();</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"><span style="color:#ABB2BF">}</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div><div class="collapsed-lines"></div></div><h2 id="maven打包设置主类" tabindex="-1"><a class="header-anchor" href="#maven打包设置主类"><span>Maven打包设置主类</span></a></h2>
<p>在<strong>pom.xml</strong>文件中插件项添加插件</p>
<div class="language-xml line-numbers-mode has-collapsed-lines collapsed" data-highlighter="shiki" data-ext="xml" style="--vp-collapsed-lines:15;background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#ABB2BF">&#x3C;</span><span style="color:#E06C75">plugin</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">	&#x3C;</span><span style="color:#E06C75">groupId</span><span style="color:#ABB2BF">>org.apache.maven.plugins&#x3C;/</span><span style="color:#E06C75">groupId</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">    &#x3C;</span><span style="color:#E06C75">artifactId</span><span style="color:#ABB2BF">>maven-assembly-plugin&#x3C;/</span><span style="color:#E06C75">artifactId</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">    &#x3C;</span><span style="color:#E06C75">executions</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">    	&#x3C;</span><span style="color:#E06C75">execution</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">        	&#x3C;</span><span style="color:#E06C75">phase</span><span style="color:#ABB2BF">>package&#x3C;/</span><span style="color:#E06C75">phase</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">            &#x3C;</span><span style="color:#E06C75">goals</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">            	&#x3C;</span><span style="color:#E06C75">goal</span><span style="color:#ABB2BF">>single&#x3C;/</span><span style="color:#E06C75">goal</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">            &#x3C;/</span><span style="color:#E06C75">goals</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">            &#x3C;</span><span style="color:#E06C75">configuration</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">            	&#x3C;</span><span style="color:#E06C75">archive</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">                	&#x3C;</span><span style="color:#E06C75">manifest</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">                    	&#x3C;</span><span style="color:#E06C75">mainClass</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">                        	xyz.hmz.backend.BackendApplication  # 替换为启动类</span></span>
<span class="line"><span style="color:#ABB2BF">                        &#x3C;/</span><span style="color:#E06C75">mainClass</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">                    &#x3C;/</span><span style="color:#E06C75">manifest</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">                &#x3C;/</span><span style="color:#E06C75">archive</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">            	&#x3C;</span><span style="color:#E06C75">descriptorRefs</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">                	&#x3C;</span><span style="color:#E06C75">descriptorRef</span><span style="color:#ABB2BF">>jar-with-dependencies&#x3C;/</span><span style="color:#E06C75">descriptorRef</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">                &#x3C;/</span><span style="color:#E06C75">descriptorRefs</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">            &#x3C;/</span><span style="color:#E06C75">configuration</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">        &#x3C;/</span><span style="color:#E06C75">execution</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">    &#x3C;/</span><span style="color:#E06C75">executions</span><span style="color:#ABB2BF">></span></span>
<span class="line"><span style="color:#ABB2BF">&#x3C;/</span><span style="color:#E06C75">plugin</span><span style="color:#ABB2BF">></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div><div class="collapsed-lines"></div></div><h2 id="个人习惯" tabindex="-1"><a class="header-anchor" href="#个人习惯"><span>个人习惯</span></a></h2>
<p>接收参数对象命名为DTO</p>
</div></template>


